//
//  MenuViewController.m
//  Restaurant
//
//  Created by wanghuan on 15/4/24.
//  Copyright (c) 2015年 wanghuan. All rights reserved.
//

#import "MenuViewController.h"

@interface MenuViewController () {

    NSArray *titleArr;
    NSArray *imageArr;
    NSArray *imageSelecteArr;
    UIImageView *imageView;
}

@end

@implementation MenuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    NSArray *controllerArr = @[@"ScanViewController",@"AllOrderViewController",@"MyViewController"];
    titleArr = @[@"菜单",@"订单",@"我的"];
    
    imageArr = @[@"cd1.png",@"dd1.png",@"user1.png"];
    imageSelecteArr = @[@"cd2.png",@"dd2.png",@"user2.png"];
    
    NSMutableArray *vcArr = [NSMutableArray array];
    
    for (int i = 0; i < controllerArr.count; i++) {
        Class con = NSClassFromString(controllerArr[i]);
        
        UIViewController *vc = [[con alloc]init];
        
        UINavigationController *nc = [[UINavigationController alloc]initWithRootViewController:vc];
        nc.navigationBar.hidden = YES;
        [vcArr addObject:nc];
        
    }
    
    self.viewControllers = vcArr;
    
    [self createTabBar];
}

- (void)createTabBar {

    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, APPWith, 49)];
    
    imageView.userInteractionEnabled = YES;
    
    imageView.backgroundColor = [UIColor whiteColor];
    
    
    for (int i = 0; i< [titleArr count] ;i++) {
        
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(40+i*(APPWith-120)/2 , 0, 36, 36)];
        [btn setBackgroundImage:[UIImage imageNamed:imageArr[i]] forState:UIControlStateNormal];
        
        btn.tag = i+1;
        
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [btn setBackgroundImage:[UIImage imageNamed:imageSelecteArr[i]] forState:UIControlStateSelected];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(btn.frame.origin.x, btn.frame.size.height+btn.frame.origin.y, btn.frame.size.width, 49-btn.frame.size.height+btn.frame.origin.y)];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = titleArr[i];
        
        label.tag = i+101;
        
        label.textColor = [UIColor grayColor];
        
        label.font = [UIFont systemFontOfSize:11];
        
        [imageView addSubview:btn];
        
        [imageView addSubview:label];
        
        if (!i) {
            btn.selected = YES;
            label.textColor = [UIColor redColor];
        }
        
    }
    
    
    [self.tabBar addSubview:imageView];


}

- (void)btnClick:(UIButton *)sender {
    
    

    for (int i = 0; i < 3; i++) {
        UIButton *button = (UIButton *)[self.tabBar viewWithTag:i+1];
        button.selected = NO;
        
        UILabel *label = (UILabel *)[self.tabBar viewWithTag:i+101];
        label.textColor = [UIColor grayColor];
    }
    
    sender.selected = YES;

    
    UILabel *label = (UILabel *)[self.tabBar viewWithTag:sender.tag+100];
    label.textColor = [UIColor redColor];
    
    self.selectedIndex = sender.tag - 1;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
